import QtQuick 1.0
import "../Delegates/"
import "../WebAccess/PictureAPI.js" as PictureAPI
import "../Util/Util.js" as Util

//つぶやき系データ用
ListViewBase {
    id: _root
    x: 0
    y: 0
    height: parent.height
    width: parent.width

    scrollBarBaseColor: _Setting.scrollBarBaseColor
    scrollBarColor: _Setting.scrollBarColor
    scrollBarOpacity: _Setting.scrollBarOpacity

    density: _Device.density

    // 左クリックした時の処理
    // 必須の共通メソッド
    // Item object = currentItem;   これデリゲート
    function clickLeft(object){
        // クリックアイテムをハイライトする
        currentIndex = indexAt(object.x, object.y);
    }

    // 右クリックした時の処理
    function clickRight(object){
        //情報を一時保存
        //_selectItemInfo.itemId = itemId;
        _selectItemInfo.screenName = object.screenName;
        _selectItemInfo.message = object.itemText;//Util.decodeHtmlSymbol();
        _selectItemInfo.isFavorite = (object.isFavorite === "true") ? true : false;
        _selectItemInfo.isFollow = (object.isFollowing === "true") ? true : false;
        // 位置を作る
        if(_selectItemInfo.mouse_x < 0){
            _selectItemInfo.mouse_x = object.width/2;
        }
        if(_selectItemInfo.mouse_y < 0){
            _selectItemInfo.mouse_y = object.y + _root.parent.y + object.height/3;
        }

        //メニューを出す
        _menuContainer.state = "MenuContextUsers";

    }

    delegate: Delegate{
        id: _delegate
        width: _root.width
        acceptedButtons: Qt.LeftButton | Qt.RightButton

        //itemIndex: _status_id
        itemId: makeItemId()
        itemTitle: makeItemTitle()
        itemMessage: makeItemMessage()
        itemInformation: makeItemInfomation()
        iconPath: _icon

        //保存用
        property string screenName: _screen_name        //Screen name
        property string inReplyToStatusId: _in_reply_to_status_id
        property string inReplyToScreenName: _in_reply_to_screen_name
        property string itemText: _text
        property string isFavorite: (_favorited === undefined) ? "false" : _favorited
        property string isFollowing: _following

        property variant urlList: null      //URLのリスト

        //IDを成形する
        function makeItemId(){
            return _status_id;
        }

        //タイトルを成形する
        function makeItemTitle(){
            return _screen_name + " / " + _name;
        }

        //本文を成形する
        function makeItemMessage(){
            var text = _text;

            //URLを正規表現で探す
            var urls = Util.pickUpUrls(_urls);
            if(urls === null){
            }else{
                //画像があれば指定する
                PictureAPI.getPcitureUrl(urls, this);
                //URLを保存
                urlList = urls;

                //URLをリンクにする
                for(var i=0; i<urls.length; i++){
                    text = text.replace(urls[i], "<a href='" + urls[i] + "'>" + urls[i] + "</a>");
                }

            }

            //記号を戻す
//            text = Util.decodeHtmlSymbol(text) + "<br />\n";
            text = text + "<br />\n";

            //場所
            text += "Location : " + _location + "<br />\n";
            text += "Bio : " + _bio;

            return text;
        }

        //情報を成形する
        function makeItemInfomation(){
            var ret = "";

            ret += "Friends : " + _friends_count
                    + " / Followers : " + _followers_count;
            ret += "<br />\nStatusCount : " + _statuses_count;
            if(_following === "true"){
                ret += "<br />\nFollowing"
            }

            return ret;
        }

        onClicked: {
//            Util.outputDebugString("onClicked:" + prevPressedButtons
//                                   + ",index=" + currentIndex
//                                   + ",itemId=" + itemId);
            // 上位にクリックイベント通す
            _root.itemClicked(mouse);

            // クリックしたらフォーカス移動
            _root.forceActiveFocus();

            switch(itemIndex){
            case 0:
                break;
            case 1:
                break;
            case 2:
                break;
            default:
                break;
            }

            //マウス情報保存
            // _root.parent.parent.parent = _loaderContent
            _selectItemInfo.mouse_x = _root.parent.parent.parent.x + mouse.x;
            _selectItemInfo.mouse_y = mouse.y + _delegate.y + _root.parent.y - contentY;
            _selectItemInfo.container = _root.parent;

            //次の動作
            switch(prevPressedButtons){
            case Qt.LeftButton:
                clickLeft(_delegate);
                break;
            case Qt.RightButton:
                clickRight(_delegate);
                break;
            default:
                break;
            }
        }

//        MouseArea {
//            anchors.fill: parent
//            acceptedButtons: Qt.LeftButton | Qt.RightButton
//        }
    }

}
